
package makai.canvas;

import java.io.Serializable;
import makai.image.MakaiImage;
import makai.image.MakaiImageMask;

/**
 * TODO: Description
 * 
 * @author Krechevskoy
 * @version 1.0
 */
public class Layer implements Cloneable, Serializable {
    /** A unique identifier for each layer  */
    private static int      SERIAL_ID   = 0;
    
    /** The identifier of this layer        */
    private final int       id;
    
    /** The name of this layer              */
    private String          name;
    
    /** The image backend of this layer     */
    private MakaiImage      image;
    
    /** The image mask for this layer       */
    private MakaiImageMask  mask;
    
    public Layer(int width, int height) {
        this("New Layer", width, height);
    }
    
    public Layer(String name, int width, int height) {
        this(name, new MakaiImage(width, height));
    }
    
    public Layer(MakaiImage image) {
        this("New Layer", image);
    }
    
    public Layer(String name, MakaiImage image) {
        // Sets the layer's ID and increments the serial ID
        this.id = ++SERIAL_ID;
        
        this.name = name;
        this.image = image;
    }
    
    public Layer(String name, MakaiImage image, MakaiImageMask mask) {
        this(name, image);
        this.mask = mask;
    }
    
    /**
     * Constructs a copy of the given layer
     * 
     * NOTE: The copy has the same ID, so it should never be added to the same Canvas
     */
    public Layer(Layer layer){
        this.id     = layer.id;
        this.name   = layer.name;
        this.image  = layer.image;
        this.mask   = layer.mask;
    }
    
    public void setImage(MakaiImage image){
        this.image = image;
    }
    
    public MakaiImage getImage(){
        return image;
    }
    
    public MakaiImage getMaskedImage(){
        if(mask==null){
            return image;
        } else {
            return mask.getMaskedImage(image);
        }
    }
    
    public void setImageMask(MakaiImageMask mask){
        this.mask = mask;
    }
    
    public MakaiImageMask getImageMask(){
        return mask;
    }
    
    public int getID(){
        return id;
    }
    
    @Override
    protected Layer clone() {        
        return new Layer(this);
    }
    
}
